/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.logic.mail;

import java.io.IOException;

import java.util.Properties;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;




public class ReaderEmailManager {
    String userEmail, emailPassword;

    ReaderEmailProtocolProperties emailProtocol;

    public ReaderEmailManager(String userEmail, String emailPassword){
        this.userEmail = userEmail ;
        this.emailPassword = emailPassword;
    }
    
    private String getHostNameEmail(){
        final int accountType =1;
        String [] hostInformation = userEmail.split("@");
        return "pop."+ hostInformation[accountType];
    }


    private Session configureProportiesSession(){
            Properties emailProperties = new Properties();
            emailProperties.setProperty(emailProtocol.EMAIL_TLS_PROTOCOL_PROPERTIE, emailProtocol.IS_TLS_ENABLE);
            emailProperties.setProperty(emailProtocol.SOCKET_SSL_PROPERTIE, emailProtocol.CLASS_JAVA_SOCKET_SSL_PROPERTIE);
            emailProperties.setProperty(emailProtocol.ERROR_SOCKET_SSL_PROPERTIE, emailProtocol.IS_NORMAL_SOCKET_USED);
            emailProperties.setProperty(emailProtocol.PROTOCOL_PORT_PROPORTIE, emailProtocol.PROTOCOL_PORT);
            emailProperties.setProperty(emailProtocol.SOCKET_SSL_PORT_PROPERTIE, emailProtocol.PROTOCOL_PORT);
            Session emailSession = Session.getDefaultInstance(emailProperties);
            return emailSession;
        }

    public void configureReaderEmailSession() throws MessagingException, IOException{//sessionLogin
        try {
            //Linker es que hace la coneccion.
            Store linker = configureProportiesSession().getStore(emailProtocol.PROTOCOL_NAME);
            linker.connect(getHostNameEmail(), userEmail, emailPassword);
            Folder emailFolder = linker.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            getMessagesEmail(emailFolder);
            emailFolder.close(false);
            linker.close();
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(ReaderEmailManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

        private void getMessagesEmail(Folder emailFolder) throws MessagingException, IOException{

            Message[] messages = emailFolder.getMessages();
            for (int indice = 0; indice < messages.length; indice++){
                System.out.println( "From:" + messages[indice].getFrom()[0].toString());
                System.out.println("Subject:" + messages[indice].getSubject());
                showMessages(messages[indice]);
            }

        }

        private void showMessages(Part partMessageEmail) throws MessagingException, IOException   {
            if (partMessageEmail.isMimeType("text/*"))
                {
                    System.out.println("Text " + partMessageEmail.getContentType());
                    System.out.println(partMessageEmail.getContent());
                    System.out.println("---------------------------------");
                }
        }

}
